import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemTenantPackageApi } from '#/api/system/tenant-package';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getTenantPackageList } from '#/api/system/tenant-package';
import { getRangePickerDefaultProps } from '#/utils';

import { $t } from '#/locales';

/** 关联数据 */
let tenantPackageList: SystemTenantPackageApi.TenantPackage[] = [];
getTenantPackageList().then((data) => (tenantPackageList = data));

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.system.tenant.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.tenant.namePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'packageId',
      label: $t('views.system.tenant.package'),
      component: 'ApiSelect',
      componentProps: {
        api: () => getTenantPackageList(),
        labelField: 'name',
        valueField: 'id',
        placeholder: $t('views.system.tenant.packagePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'contactName',
      label: $t('views.system.tenant.contactName'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.tenant.contactNamePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'contactMobile',
      label: $t('views.system.tenant.contactMobile'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.tenant.contactMobilePlaceholder'),
      },
      rules: 'mobile',
    },
    {
      label: $t('views.system.tenant.username'),
      fieldName: 'username',
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.tenant.usernamePlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['id'],
        show: (values) => !values.id,
      },
    },
    {
      label: $t('views.system.tenant.password'),
      fieldName: 'password',
      component: 'InputPassword',
      componentProps: {
        placeholder: $t('views.system.tenant.passwordPlaceholder'),
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['id'],
        show: (values) => !values.id,
      },
    },
    {
      label: $t('views.system.tenant.accountCount'),
      fieldName: 'accountCount',
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.system.tenant.accountCountPlaceholder'),
      },
      rules: 'required',
    },
    {
      label: $t('views.system.tenant.expireTime'),
      fieldName: 'expireTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
        placeholder: $t('views.system.tenant.expireTimePlaceholder'),
      },
      rules: 'required',
    },
    {
      label: $t('views.system.tenant.websites'),
      fieldName: 'websites',
      component: 'Select',
      componentProps: {
        placeholder: $t('views.system.tenant.websitesPlaceholder'),
        mode: 'tags',
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.system.tenant.name'),
      component: 'Input',
      componentProps: {
	placeholder: $t('views.system.tenant.namePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'contactName',
      label: $t('views.system.tenant.contactName'),
      component: 'Input',
      componentProps: {
	placeholder: $t('views.system.tenant.contactNamePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'contactMobile',
      label: $t('views.system.tenant.contactMobile'),
      component: 'Input',
      componentProps: {
	placeholder: $t('views.system.tenant.contactMobilePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
	placeholder: $t('common.statusPlaceholder'),
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
    },
    {
      fieldName: 'createTime',
      label: $t('common.createTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: $t('views.common.id'),
      minWidth: 100,
    },
    {
      field: 'name',
      title: $t('views.system.tenant.name'),
      minWidth: 180,
    },
    {
      field: 'packageId',
      title: $t('views.system.tenant.package'),
      minWidth: 180,
      formatter: ({ cellValue }) => {
        return cellValue === 0
          ? $t('views.system.tenantPackage.defaultName')
          : tenantPackageList.find((pkg) => pkg.id === cellValue)?.name || '-';
      },
    },
    {
      field: 'contactName',
      title: $t('views.system.tenant.contactName'),
      minWidth: 100,
    },
    {
      field: 'contactMobile',
      title: $t('views.system.tenant.contactMobile'),
      minWidth: 180,
    },
    {
      field: 'accountCount',
      title: $t('views.system.tenant.accountCount'),
      minWidth: 100,
    },
    {
      field: 'expireTime',
      title: $t('views.system.tenant.expireTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'websites',
      title: $t('views.system.tenant.websites'),
      minWidth: 180,
    },
    {
      field: 'status',
      title: $t('common.status'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 130,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
